#!/bin/bash

REDIS_HOST='127.0.0.1'
REDIS_PORT=6379
REDIS_PASSWORD=''
REDIS_CLI="/usr/local/redis/bin/redis-cli"

cmd_list="redis_status\nredis_used_memory\nredis_data_used_memory\nredis_peak_memory\nredis_maxmemory\nredis_total_connections\nredis_total_commands\nredis_ops_persec\nredis_rejected_connections\nredis_hits\nredis_misses\nredis_hit_rate\nredis_expired_keys\nredis_cur_connections\nredis_input_bytes\nredis_output_bytes\n"

# 判断Redis是否有连接密码，定义连接Redis命令
if [ ! -z "${REDIS_PASSWORD}" ];then
        CMD="${REDIS_CLI} -h ${REDIS_HOST} -p ${REDIS_PORT} -a ${REDIS_PASSWORD}"
else
        CMD="${REDIS_CLI} -h ${REDIS_HOST} -p ${REDIS_PORT}"
fi
# echo ${CMD}

## 服务状态
# 是否正常使用
redis_status(){
        res=$(${CMD} ping 2> /dev/null)
        if [ ${res} == "PONG" ];then
                echo "1"
        else
                echo "0"
        fi
}

## 内存相关
# 实际使用内存(字节)
redis_used_memory(){
        ${CMD} info memory 2> /dev/null | grep "used_memory_rss:" | awk -F: '{print $2}'
}

# 数据占用内存(字节)
redis_data_used_memory(){
        ${CMD} info memory 2>/dev/null | grep "used_memory_dataset:" | awk -F: '{print $2}'
}

# 峰值使用内存(服务+数据)
redis_peak_memory(){
        ${CMD} info memory 2> /dev/null | grep "used_memory_peak:" | awk -F: '{print $2}'
}

# 最大限制内存
redis_maxmemory(){
        ${CMD} info memory | grep "maxmemory:" | awk -F: '{print $2}'
}

## 服务相关
# 连接总数
redis_total_connections(){
        ${CMD} info stats 2> /dev/null | grep "total_connections_received:" | awk -F: '{print $2}'
}

# 处理命令总数
redis_total_commands(){
        ${CMD} info stats 2>/dev/null | grep "total_commands_processed:" | awk -F: '{print $2}'
}

# 当前每秒操作数
redis_ops_persec(){
        ${CMD} info stats 2>/dev/null | grep "instantaneous_ops_per_sec:" | awk -F: '{print $2}'
}

# 拒绝连接数
redis_rejected_connections(){
        ${CMD} info stats 2>/dev/null | grep "rejected_connections:" | awk -F: '{print $2}'
}

# 命中键数
redis_hits(){
        ${CMD} info stats 2>/dev/null | grep "keyspace_hits:" | awk -F: '{print $2}'
}

# 未命中键数
redis_misses(){
        ${CMD} info stats 2>/dev/null | grep "keyspace_misses:" | awk -F: '{print $2}'
}

# 命中率
redis_hit_rate(){
        HIT=$(${CMD} info stats 2>/dev/null | grep "keyspace_hits:" | awk -F: '{print $2}' | sed 's/[^0-9]*//g')
        MISS=$(${CMD} info stats 2>/dev/null | grep "keyspace_misses:" | awk -F: '{print $2}'  | sed 's/[^0-9]*//g')
        TOTAL=$(( HIT + MISS ))
        if [ "${TOTAL}" -eq 0 ];then
                echo 0
        else
                echo $(( HIS/TOTAL))
        fi
}

# 过期键数
redis_expired_keys(){
        ${CMD} info stats 2>/dev/null | grep "expired_keys:" | awk -F: '{print $2}'
}
# 当前连接数
redis_cur_connections(){
        ${CMD} info clients 2>/dev/null | grep "connected_clients:" | awk -F: '{print $2}'
}

## 流量情况
# 入站流量(字节)
redis_input_bytes(){
        ${CMD} info stats 2>/dev/null | grep "total_net_input_bytes:" | awk -F: '{print $2}'
}

# 出站流量(字节)
redis_output_bytes(){
        ${CMD} info stats 2>/dev/null | grep "total_net_output_bytes:" | awk -F: '{print $2}'
}

## 主函数
if [ -z "$1" ];then
        echo -en ${cmd_list}
        exit
fi

case $1 in
redis_status)
        redis_status;;
redis_used_memory)
        redis_used_memory;;
redis_data_used_memory)
        redis_data_used_memory;;
redis_peak_memory)
        redis_peak_memory;;
redis_maxmemory)
        redis_maxmemory;;
redis_total_connections)
        redis_total_connections;;
redis_total_commands)
        redis_total_commands;;
redis_ops_persec)
        redis_ops_persec;;
redis_rejected_connections)
        redis_rejected_connections;;
redis_hits)
        redis_hits;;
redis_misses)
        redis_misses;;
redis_hit_rate)
        redis_hit_rate;;
redis_expired_keys)
        redis_expired_keys;;
redis_cur_connections)
        redis_cur_connections;;
redis_input_bytes)
        redis_input_bytes;;
redis_output_bytes)
        redis_output_bytes;;
*)
        echo "Usage: $0 {cmd_list}"
esac
